package com.avic.rong.activity.IncreaseTQ;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.api.increase_tp.UpIncreaseTpApi;
import com.avic.rong.bean.increase_tp.IncreaseTpBean;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.StringUtils;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import butterknife.BindView;

//增票资质-审核成功
public class UpIncreaseTpActivity extends BaseActivity {
    @BindView(R.id.et_companyName)
    EditText et_companyName;
    @BindView(R.id.et_identificationNumber)
    EditText et_identificationNumber;
    @BindView(R.id.et_regAddress)
    EditText et_regAddress;
    @BindView(R.id.et_regMobile)
    EditText et_regMobile;
    @BindView(R.id.et_bank)
    EditText et_bank;
    @BindView(R.id.et_blankNo)
    EditText et_blankNo;
    @BindView(R.id.btn_commit)
    Button btn_commit;
    private IncreaseTpBean data;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_up_increasetp;
    }

    @Override
    protected void initView() {
        tvTitle.setText("修改增票资质");
        ivRight.setBackgroundResource(R.mipmap.slh);

        if (getIntent()!=null) {
            data = (IncreaseTpBean) getIntent().getSerializableExtra("data");

            String companyName = data.getCompanyName();
            String regAddress = data.getRegAddress();
            String bank = data.getBank();
            String blankNo = data.getBlankNo();
            String identificationNumber = data.getIdentificationNumber();
            String regMobile = data.getRegMobile();

            et_companyName.setText(getString(companyName));
            et_regAddress.setText(getString(regAddress));
            et_bank.setText(getString(bank));
            et_blankNo.setText(getString(blankNo));
            et_identificationNumber.setText(getString(identificationNumber));
            et_regMobile.setText(getString(regMobile));

        }
    }

    public String getString(String string){
        char[] chars = string.toCharArray();
        if (chars.length>3) {
            for (int i = 1; i < chars.length - 2; i++) {
                string.replace(chars[i], '*');
            }
        }
        return string;
    }

    @Override
    protected void addListener() {
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upTp();
            }
        });
    }

    public static Intent queryIntent(Context context){
        Intent intent = new Intent(context, UpIncreaseTpActivity.class);
        return intent;
    }

    private void upTp() {
        String companyName = et_companyName.getText().toString().trim();
        String identificationNumber = et_identificationNumber.getText().toString().trim();
        String regAddressr = et_regAddress.getText().toString().trim();
        String regMobile = et_regMobile.getText().toString().trim();
        String bank = et_bank.getText().toString().trim();
        String blankNo = et_blankNo.getText().toString().trim();

        String login_token = SharedPreferencesUtil.getLoginToken();
        if (StringUtils.isEmpty(companyName)||StringUtils.isEmpty(regAddressr)||StringUtils.isEmpty(regMobile)
                ||StringUtils.isEmpty(bank)||StringUtils.isEmpty(blankNo)||StringUtils.isEmpty(login_token)) {
            ToastTools.showPublicToast("内容不能为空");
        }else {
            UpIncreaseTpApi upIncreaseTpApi = new UpIncreaseTpApi(this, increaseTpListener);
            upIncreaseTpApi.setArgument(login_token, companyName, identificationNumber, regAddressr, regMobile, bank, blankNo);
            HttpManager.getInstance().doHttpDeal(upIncreaseTpApi);
        }
    }
    HttpOnNextListener<BaseResultEntity> increaseTpListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
                //跳转等待审核界面
                startActivity(WaitAuditActivity.queryIntent(UpIncreaseTpActivity.this));
                finish();
                NotThroughActivity.finishActivity();
            }else {
                ToastTools.showPublicToast(baseResultEntity.getMsg());
            }
        }
    };

}
